#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"


//------------------------------------新的DEMURA流程------------------------------------

void VTDR6110C_GM8A0_Enable_Flash_Control_and_SPI_Flash_En(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[4]; 
	  u16 tmp;
	
      SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	//Bit 7为0 表示写操作	
			delay_ms(20);    
	
			buffer1[0] = 0x02;
      buffer1[1] = 0x59; 
			buffer1[2] = 0x11; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);/////mrak  demura  on status
			buffer1[0] = 0x02;
      buffer1[1] = 0x6F; 
			buffer1[2] = 0x03; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);///////mark RAM for demura  use
			buffer1[0] = 0x02;
      buffer1[1] = 0xBD; 
			buffer1[2] = 0x00; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);///////off TITC(RAM compression)
			delay_ms(300); 
			buffer1[0] = 0x01;
      buffer1[1] = 0x28; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);//////mark display off  ,,--the key setting
			delay_ms(20);
      buffer1[0] = 0x03;
      buffer1[1] = 0xF0; 
			buffer1[2] = 0xAA; 
			buffer1[3] = 0x10; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      buffer1[0] = 0x03;
      buffer1[1] = 0xEA; 
			buffer1[2] = 0x30; 
      buffer1[3] = 0x00;
      SSD2828_W_Array(SigMode,channel,buffer1,0);	   
			buffer1[0] = 0x02;
      buffer1[1] = 0xEE; 
			buffer1[2] = 0xAB; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      buffer1[0] = 0x02;
      buffer1[1] = 0xED; 
			buffer1[2] = 0x20; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
			delay_ms(100); 
      buffer1[0] = 0x02;      
      buffer1[1] = 0xED; 
			buffer1[2] = 0x00; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
      buffer1[0] = 0x02; 
      buffer1[1] = 0xEE; 
			buffer1[2] = 0x06; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
      buffer1[0] = 0x02; 
      buffer1[1] = 0xED; 
			buffer1[2] = 0x20; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      delay_ms(100); 	
      buffer1[0] = 0x02;       
      buffer1[1] = 0xED; 
			buffer1[2] = 0x00; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
      //read flash id
      buffer1[0] = 0x02;  
      buffer1[1] = 0xEE; 
			buffer1[2] = 0x9F; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      buffer1[0] = 0x02; 
      buffer1[1] = 0xED; 
			buffer1[2] = 0x44; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      delay_ms(100); 
      buffer1[0] = 0x02; 
      buffer1[1] = 0xED; 
			buffer1[2] = 0x00; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
          
////    /***read***/ 
//    //LP_B7_Data=0200 低速写,  LP_B7_Data|0x0080 将bit7 置1，为读操作
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);  // Bit 7为1 表示读操作  
			delay_ms(5);
			Generate_MIPI_Read(pdev,SigMode,channel,0xEC ,3, buffer+4);		

	
			buffer[0] = 0x2F;
			buffer[1] = 0x06;	

			SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
			STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);    
}


void VTDR6110C_GM8A0_Flash_Check_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
			u8 buffer1[10]; 
      
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
			delay_ms(5);                 
			
//			buffer1[0] = 0x02;
//      buffer1[1] = 0x59; 
//			buffer1[2] = 0x09;	
//      SSD2828_W_Array(SigMode,channel,buffer1,0);
	
      buffer1[0] = 0x01;
      buffer1[1] = 0x28;  
      SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(15);
	
			buffer1[0] = 0x03;
      buffer1[1] = 0xFF; 
			buffer1[2] = 0x5A; 
			buffer1[3] = 0x80; 	
      SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);  // Bit 7为1 表示读操作  
			delay_ms(5);
			Generate_MIPI_Read(pdev,SigMode,channel,0xFA ,10, buffer+4);

			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
			delay_ms(5); 
			
			buffer1[0] = 0x01;
      buffer1[1] = 0x29;  
      SSD2828_W_Array(SigMode,channel,buffer1,0);
	
			buffer[0] = 0x2F;
			buffer[1] = 0x04;	
			//			buffer[2] += 10;/////上位机的长度限制不能超过10个，目前4个byte校验足够确认flash是否正确烧录了。
			SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
			STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);
}


void VTDR6110C_GM8A0_Inter_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
        
        u8 buffer1[5]; 
        u16 tmp;
	
				SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
				delay_ms(20);
//				buffer1[0] = 0x02;
//				buffer1[1] = 0x59; 
//				buffer1[2] = 0x11; 
//				SSD2828_W_Array(SigMode,channel,buffer1,0);/////mrak  demura  on status
//				buffer1[0] = 0x02;
//				buffer1[1] = 0x6F; 
//				buffer1[2] = 0x03; 
//				SSD2828_W_Array(SigMode,channel,buffer1,0);///////mark RAM for demura  use
//				buffer1[0] = 0x02;
//				buffer1[1] = 0xBD; 
//				buffer1[2] = 0x00; 
//				SSD2828_W_Array(SigMode,channel,buffer1,0);///////off TITC(RAM compression)
//				delay_ms(300); 
//				buffer1[0] = 0x01;
//				buffer1[1] = 0x28; 
//				SSD2828_W_Array(SigMode,channel,buffer1,0);//////mark display off  ,,--the key setting
			
	
        buffer1[0] = 0x03;
        buffer1[1] = 0xF0;
        buffer1[2] = 0xAA;
        buffer1[3] = 0x10;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[0] = 0x04;
        buffer1[1] = 0xED;
        buffer1[2] = 0x00;
				buffer1[3] = 0x00;
				buffer1[4] = 0x01;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(100);
        buffer1[0] = 0x04;
        buffer1[1] = 0xED;
        buffer1[2] = 0x00;
				buffer1[3] = 0x00;
				buffer1[4] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(100);
				buffer1[0] = 0x02;
				buffer1[1] = 0x65;
        buffer1[2] = 0x05;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[0] = 0x02;
        buffer1[1] = 0xEB;
        buffer1[2] = 0x40;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[0] = 0x02;
				buffer1[1] = 0x65;
        buffer1[2] = 0x0A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				buffer1[0] = 0x02;
				buffer1[1] = 0xEA;
        buffer1[2] = 0x10;/////2F--old   10--new
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(10);

    buffer[0] = 0x2F;
		buffer[1] = 0x01;		

//		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);				////test  HS
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
}



void VTDR6110C_GM8A0_Flash_Write_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
      
}

void VTDR6110C_GM8A0_Flash_Write_Demura2(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
   
}

void VTDR6110C_GM8A0_Exit_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer2[12]; 
		u16 temp=0;
	
		delay_ms(6000);
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(20);	
	
    buffer2[0] = 0x03; 
    buffer2[1] = 0xFF;
    buffer2[2] = 0x5A;
    buffer2[3] = 0x80;
    SSD2828_W_Array(SigMode,channel,buffer2,0);
	
    buffer2[0] = 0x05; 
    buffer2[1] = 0xEE;
    buffer2[2] = 0x32;
		buffer2[3] = 0x00;
		buffer2[4] = 0x01;
		buffer2[5] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer2,0);
    delay_ms(100);
		buffer2[0] = 0x02; 
    buffer2[1] = 0xED;
    buffer2[2] = 0x02;
    SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(12000);
		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);  // Bit 7为1 表示读操作  
		delay_ms(5);
		do
		{
				
				Generate_MIPI_Read(pdev,SigMode,channel,0xEB ,10, buffer2+0);
				temp = buffer2[7];
				delay_ms(200);
//				SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);  // Bit 7为1 表示读操作  
//				delay_ms(5);
		}while(temp != 0x70);////判断是否IC program done...
		

		delay_ms(1000);//延迟1秒
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
		delay_ms(6); 
		
		buffer2[0] = 0x02; 
    buffer2[1] = 0xED;
    buffer2[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer2,0);
		
		
//		buffer2[0] = 0x02; //////////////移位还原，
//    buffer2[1] = 0x65;
//    buffer2[2] = 0x07;
//    SSD2828_W_Array(SigMode,channel,buffer2,0);			



		
    buffer[0] = 0x2F; 
    buffer[1] = 0x02;

    delay_ms(20);//延迟
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		//SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4]=0x70;//向上返回
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
}


void VTDR6110C_GM8A0_Flash_Erase_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  //flash enable
      u8 buffer1[4]; 
			u8 VTDR6110C_Read_REG = 0xEF;
	
      SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
			delay_ms(5);                 

      buffer1[0] = 0x03;
      buffer1[1] = 0xF0; 
			buffer1[2] = 0xAA; 
			buffer1[3] = 0x10; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);

      buffer1[0] = 0x03;
			buffer1[1] = 0xEA; 
			buffer1[2] = 0x30; 
      buffer1[3] = 0x01;
      SSD2828_W_Array(SigMode,channel,buffer1,0);	   
			buffer1[0] = 0x02;
      buffer1[1] = 0x65; 
			buffer1[2] = 0x06; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      buffer1[0] = 0x02;
			buffer1[1] = 0xEB; 
			buffer1[2] = 0x00; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
      buffer1[0] = 0x03;
			buffer1[1] = 0xED; 
			buffer1[2] = 0x00; 
			buffer1[3] = 0x01; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);	
			buffer1[0] = 0x03;
			buffer1[1] = 0xED; 
			buffer1[2] = 0x00; 
			buffer1[3] = 0x00; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);
	    delay_ms(100);			
			buffer1[0] = 0x03;
			buffer1[1] = 0xED; 
			buffer1[2] = 0x00; 
			buffer1[3] = 0x10; 
      SSD2828_W_Array(SigMode,channel,buffer1,0);   
      delay_ms(5000);	////实测满数据擦除耗时4.6s，保险起见，设定5s延时
//			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); delay_ms(15);
//			do
//			{   
//				Generate_MIPI_Read(pdev,SigMode,channel,VTDR6110C_Read_REG ,1, buffer+4);
//				delay_ms(6);     
//			}while((buffer[4]&0x01)==0x01);
   
      	
      buffer1[0] = 0x03;
      buffer1[1] = 0xED;
			buffer1[2] = 0x00;
      buffer1[3] = 0x00;
      SSD2828_W_Array(SigMode,channel,buffer1,0);
      //不返回

		
		buffer[0] = 0x2F;
		buffer[1] = 0x05;		
		buffer[4] = 0x00;	
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú	
}




void VTDR6110C_GM8A0_Demura_Function_ON(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5]; 
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);	
		buffer1[0] = 0x02;
		buffer1[1] = 0x59;
		buffer1[2] = 0x01;
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		delay_ms(5);
	
    buffer[0] = 0x2F;
		buffer[1] = 0x07;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 	
}

void VTDR6110C_GM8A0_Demura_Function_OFF(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5]; 
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);	
		buffer1[0] = 0x02;
		buffer1[1] = 0x59;
		buffer1[2] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		delay_ms(5);
	
    buffer[0] = 0x2F;
		buffer[1] = 0x08;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  	
}




void ProcessForDmuR16( USB_OTG_CORE_HANDLE *pdev,uint8_t ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)//ADD BY PCS
{
  switch(buffer[1])
    {
  
			case 0x01:                                      //写寄存器
					VTDR6110C_GM8A0_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					VTDR6110C_GM8A0_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					VTDR6110C_GM8A0_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					VTDR6110C_GM8A0_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					VTDR6110C_GM8A0_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					VTDR6110C_GM8A0_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					VTDR6110C_GM8A0_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					VTDR6110C_GM8A0_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					VTDR6110C_GM8A0_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;	

    }
}